package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.CourseSchedulingAlways;
import com.xmy.cultivate.mapper.CourseSchedulingAlwaysMapper;
import com.xmy.cultivate.service.ICourseSchedulingAlwaysService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 日期总排课表，计算缺课使用 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2023-07-28
 */
@Service
public class CourseSchedulingAlwaysServiceImpl extends ServiceImpl<CourseSchedulingAlwaysMapper, CourseSchedulingAlways> implements ICourseSchedulingAlwaysService {

    @Autowired
    CourseSchedulingAlwaysMapper courseSchedulingAlwaysMapper;

    /**
     *
     * @param yearPart 年份
     * @param quarterNum 学期
     * @param gradeSection 学段
     * @param num 课次
     * @return
     */
    public LocalDate getStartDate(Integer yearPart,Integer quarterNum,Integer gradeSection,Integer num){
        QueryWrapper<CourseSchedulingAlways> courseSchedulingAlwaysQueryWrapper = new QueryWrapper<>();
        courseSchedulingAlwaysQueryWrapper.select("MIN(start_date) as start_date");
        courseSchedulingAlwaysQueryWrapper.eq("year_part",yearPart);
        courseSchedulingAlwaysQueryWrapper.eq("quarter_num",quarterNum);
        courseSchedulingAlwaysQueryWrapper.eq("grade_section",gradeSection);
        courseSchedulingAlwaysQueryWrapper.eq(num != null,"num",num);
        courseSchedulingAlwaysQueryWrapper.last("limit 1");
        CourseSchedulingAlways courseSchedulingAlways = courseSchedulingAlwaysMapper.selectOne(courseSchedulingAlwaysQueryWrapper);
        if (courseSchedulingAlways !=null){
            return courseSchedulingAlways.getStartDate();
        }else {
            return null;
        }
    }

    /**
     * 获取结束日期
     *
     * @param yearPart 年份
     * @param quarterNum 学期
     * @param gradeSection 学段
     * @param num 课次
     * @return
     */
    public LocalDate getEndDate(Integer yearPart,Integer quarterNum,Integer gradeSection,Integer num){
        QueryWrapper<CourseSchedulingAlways> courseSchedulingAlwaysQueryWrapper = new QueryWrapper<>();
        courseSchedulingAlwaysQueryWrapper.select("MAX(end_date) as end_date");
        courseSchedulingAlwaysQueryWrapper.eq("year_part",yearPart);
        courseSchedulingAlwaysQueryWrapper.eq("quarter_num",quarterNum);
        courseSchedulingAlwaysQueryWrapper.eq("grade_section",gradeSection);
        courseSchedulingAlwaysQueryWrapper.eq(num!=null,"num",num);
        courseSchedulingAlwaysQueryWrapper.last("limit 1");
        CourseSchedulingAlways courseSchedulingAlways = courseSchedulingAlwaysMapper.selectOne(courseSchedulingAlwaysQueryWrapper);
        if (courseSchedulingAlways !=null){
            return courseSchedulingAlways.getEndDate();
        }else {
            return null;
        }
    }

}
